﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLThoiHoc
    {
         List<ThoiHoc> L;
         QuanLySVDataContext db;
        

        // constructor
        public BLLThoiHoc()
        {
            this.L = new QuanLySVDataContext().ThoiHocs.ToList();
            this.db = new QuanLySVDataContext();
        }
              
        // kiem tra 1 sinh vien co ton tai hay khong
        public bool checkExist(ThoiHoc temp)
        {
            foreach (ThoiHoc dt in L)
            {
                if (dt.Equals(temp.SVID))
                    return true;
            }
            return false;
        }

        // update thong tin thoi hoc
        public bool updateThoiHoc(ThoiHoc update)
        {
            try
            {                

                var temp = (from c in db.ThoiHocs
                            where c.SVID == update.SVID
                            select c).First();
                ThoiHoc found = (ThoiHoc)temp;
                found.SVID = update.SVID;
                found.SoQDThoiHoc = update.SoQDThoiHoc;
                found.NgayThoiHoc = update.NgayThoiHoc;
                found.LyDoThoiHoc = update.LyDoThoiHoc;

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them sinh vien bi thoi hoc moi vao database
        public bool insertThoiHoc(ThoiHoc temp)
        {
            try
            {
                db.ThoiHocs.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa sinh vien bi thoi hoc
        public bool deleteThoiHoc(ThoiHoc temp)
        {
            try
            {
                var dt = (from c in db.ThoiHocs
                          where c.SVID == temp.SVID
                          select c).First();
                ThoiHoc found = (ThoiHoc)dt;

                db.ThoiHocs.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //tim kiem sinh vien
        public object BindingSV(string SV)
        {
            return from c in db.SinhViens
                   where c.SVID == SV
                   select c;
        }

          // tra ve danh sach cac sinh vien bi thoi hoc
        public List<ThoiHoc> returnListThoiHoc()
        {
            this.L= new QuanLySVDataContext().ThoiHocs.ToList();
            return L;
        }

        // tra ve 1 sinh vien bi thoi hoc theo so thu tu trong list
        public ThoiHoc retrieveByIndex(int i)
        {
            this.L = new QuanLySVDataContext().ThoiHocs.ToList();
            if (i >= 0 && i < L.Count)
                return L[i];
            else return null;
        }

        // tra ve 1 sinh vien  bi thoi hoc theo ma sinh vien
        public ThoiHoc retrieveByID(string ID)
        {
            this.L = new QuanLySVDataContext().ThoiHocs.ToList();
            foreach (ThoiHoc temp in L)
                if (temp.SVID == ID)
                {
                    return temp;
                }
            return null;
        }
        
        // kiem tra thong tin 1 sinh vien co hop le hay khong
        public bool kiemTra(ThoiHoc temp)
        {
            if (temp.SVID == "") return false;
            return true;
        }
    }
}
